package com.zhiwei.msf.gateway.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiwei.msf.common.constant.CommonErrorConstants;
import com.zhiwei.msf.common.constant.ResultResponseMsg;
import com.zhiwei.msf.common.exception.BasePublishException;
import com.zhiwei.msf.common.util.BeanConverterUtil;
import com.zhiwei.msf.gateway.controller.vo.QueryUserInfoVo;
import com.zhiwei.msf.gateway.controller.vo.UserInfoVo;
import com.zhiwei.msf.gateway.controller.vo.UserLoginVo;
import com.zhiwei.msf.gateway.controller.vo.UserLogoutVo;
import com.zhiwei.msf.gateway.controller.vo.UserRegisterVo;
import com.zhiwei.msf.gateway.dao.entity.UserInfo;
import com.zhiwei.msf.gateway.dto.QueryUserInfoDto;
import com.zhiwei.msf.gateway.dto.UserRegisterDto;
import com.zhiwei.msf.gateway.service.LoginService;
import com.zhiwei.msf.gateway.service.UserInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * function: 用户服务控制器
 * author: zhiwei_yang
 * time: 2020/2/15-19:41
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户服务")
public class UserController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户登陆服务
     *
     * @param userLoginVo 用户登陆信息
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登陆")
    public ResultResponseMsg login(@RequestBody UserLoginVo userLoginVo, HttpSession httpSession) {
        String username = userLoginVo.getUsername();
        String password = userLoginVo.getPassword();
        String captcha = userLoginVo.getCaptcha();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(captcha)) {
            throw new BasePublishException(CommonErrorConstants.PARAM_ERROR);
        }

        return loginService.userLogin(username, password, httpSession.getId(), captcha);
    }

    /**
     * 退出登陆
     *
     * @param userLogoutVo 退出登陆用户信息
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登陆")
    public ResultResponseMsg logout(@RequestBody UserLogoutVo userLogoutVo, HttpSession httpSession) {
        if (StringUtils.isEmpty(userLogoutVo.getToken())) {
            throw new BasePublishException(CommonErrorConstants.PARAM_ERROR);
        }
        loginService.userLogout(userLogoutVo.getToken(), httpSession.getId());
        return ResultResponseMsg.buildSuccessResultMsg();
    }

    /**
     * 用户注册服务
     *
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResultResponseMsg register(@RequestBody UserRegisterVo userRegisterVo, HttpSession httpSession) {
        String username = userRegisterVo.getUsername();
        String password = userRegisterVo.getPassword();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new BasePublishException(CommonErrorConstants.PARAM_ERROR);
        }
        return loginService.userRegister(BeanConverterUtil.convertToBean(userRegisterVo, UserRegisterDto.class),
                httpSession.getId());
    }

    /**
     * 用户更新
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("用户更新")
    public ResultResponseMsg update(@RequestBody UserInfoVo userInfoVo) {
        UserInfo userInfo = BeanConverterUtil.convertToBean(userInfoVo, UserInfo.class);
        userInfoService.updateUser(userInfo);
        return ResultResponseMsg.buildSuccessResultMsg();
    }

    /**
     * 用户注销
     *
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("用户注销")
    public ResultResponseMsg delete(String userId) {
        userInfoService.deleteUser(userId);
        return ResultResponseMsg.buildSuccessResultMsg();
    }

    /**
     * 用户信息搜索
     *
     * @return
     */
    @PostMapping("/search")
    @ApiOperation("用户信息搜索")
    public ResultResponseMsg search(@RequestBody QueryUserInfoVo queryUserInfo) {
        return ResultResponseMsg.buildSuccessResultMsg(
                userInfoService.search(BeanConverterUtil.convertToBean(queryUserInfo, QueryUserInfoDto.class)));
    }
}